// pages/apply/apply.js
import { Apply } from '../../model/apply.js'
const apply = new Apply();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:'',
        questionList:[],//大列表
        optionId:'',//IDstring
        questionId:'',//问题ID
        classId:'',//活动id
        // apply:{},
        isJoin:0,
        apply:{},
        questionOptionName:'请简答'
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        id: options.id,
        isJoin:options.isJoin
      })
      this.applyForm()
    },
    // 返回
    handlerGobackClick() {
      wx.navigateBack({
        delta: 1
      })
    },
    // 单选
    radioChange(e){
      // console.log(e.currentTarget.dataset.item.id)
      var arr = []
      arr.push(e.detail.value)
      var list = this.data.questionList
      for(var i in list){
        if (list[i].id == e.currentTarget.dataset.item.id){
          list[i].anslist = arr
        }
      }
      this.setData({
        questionList: list
      })
    },
    // 选择
    checkboxChange: function (e) {
      // console.log(e)
      var list = this.data.questionList
      var t = e.currentTarget.dataset.index
      for (var i in list) {
        if (list[i].id == e.currentTarget.dataset.item.id) {
          // for (var t in list[i].optionList){
            list[i].optionList[t].checked = !list[i].optionList[t].checked
            if (list[i].optionList[t].checked) {
              list[i].anslist.push(list[i].optionList[t].id)
            }
            else {
              for (var j in list[i].anslist) {
                if (list[i].optionList[t].id == list[i].anslist[j]) {
                  list[i].anslist.splice(j, 1)
                }
              }
            }
          // }
        }
      }
      this.setData({
        questionList: list
      })
    },
    // 简答
    bytext(e){
      console.log(e)
      var list = this.data.questionList
      for (var i in list) {
        if (list[i].id == e.currentTarget.dataset.item.id) {
          list[i].anstext = e.detail.value
        }
      }
      this.setData({
        questionList: list
      })
    },
    //报名表单
    applyForm(){
        var obj={id:this.data.id}
        apply.activity_question(obj).then(res=>{
            console.log(res)
            for(var i=0;i<res.data.questionInfoList.length;i++){
              res.data.questionInfoList[i].isChoses = 0
              if(res.data.questionInfoList[i].type!=3){
                res.data.questionInfoList[i].anslist = []
                for(var j=0;j<res.data.questionInfoList[i].optionList.length;j++){
                  res.data.questionInfoList[i].optionList[j].checked=false
                  if(res.data.questionInfoList[i].optionList[j].isChose ==1){
                    res.data.questionInfoList[i].isChoses = 1
                  }
                }
              }
              else{i
                res.data.questionInfoList[i].anstext = ''
              }
              if(res.data.questionInfoList[i].questionOptionName!=null && res.data.questionInfoList[i].questionOptionName!=''){
                this.setData({
                  questionOptionName:res.data.questionInfoList[i].anstext
                })
    
              }
            }
            this.setData({
              questionList: res.data.questionInfoList,
              classId: res.data.questionInfoList[0].classId,
              apply:res.data
            })
            console.log(this.data.questionList)
        }).catch(error=>{

            console.log(error)
        })
        
    },  
    //报名参数
    onsubmits(){
      var that=this
      var obj = []
      for (var i in that.data.questionList){
        var msg ={}
        if (that.data.questionList[i].type ==1){
          //单选
          msg = {
            questionInfoId: that.data.questionList[i].id,
            optionIds: that.data.questionList[i].anslist,
            remark:'',
          }
          if (that.data.questionList[i].anslist.length == 0){
            wx.showToast({
              title: '请作答全部选择题',
              icon:'none'
            })
            return false
          }
          obj.push(msg)
        }
        else if (that.data.questionList[i].type == 2){
          //多选
          msg = {
            questionInfoId: that.data.questionList[i].id,
            optionIds: that.data.questionList[i].anslist,
            remark: '',
          }
          if (that.data.questionList[i].anslist.length == 0) {
            wx.showToast({
              title: '请作答全部选择题',
              icon: 'none'
            })
            return false
          }
          obj.push(msg)
        }
        else{
          //简答
          msg = {
            questionInfoId: that.data.questionList[i].id,
            optionIds: [],
            remark: that.data.questionList[i].anstext,
          }
          // if (that.data.questionList[i].anstext == '') {
          //   wx.showToast({
          //     title: '请作答简答题',
          //     icon: 'none'
          //   })
          //   return false
          // }
          obj.push(msg)
        }
      }
      var parmes = {
        id:that.data.id,
        json:JSON.stringify(obj)
      }
      wx.showModal({
        title: '提示',
        content: '您确定要提交报名吗',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            that.apply_yes(parmes)
            console.log(parmes)
            
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      
    },

    //报名
    apply_yes(parmes){
      var that=this
      apply.join_activity(parmes).then(res=>{
        console.log(res)
        if(res.code==0 && res.data.notice!=''){
          that.setData({
            isJoin:1
          })
          wx.showModal({
            cancelColor: 'cancelColor',
            title: '提示',
            content: res.data.notice,
            showCancel:false,
            success (res) {
              if (res.confirm) {
              console.log('用户点击确定')
                wx.reLaunch({
                  url: '../activity_signup/activity_signup?id='+that.data.id
                })
              }
            }
          })
         
        }else if(res.code==1){
          wx.showModal({
            cancelColor: 'cancelColor',
            title: '提示',
            content: res.msg,
            showCancel:false,
            success (res) {
              if (res.confirm) {
              console.log('用户点击确定')
              wx.reLaunch({
                url: '/pages/activity_signup/activity_signup?id='+that.data.id,
              });
              }
            }
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration:1000,
          })
          return false
        }
      }).catch(error=>{
        console.log(error)
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})